package com.his.struts.action.yljg;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.DynaActionForm;

import com.his.dao.MedicalInstitDAO;
import com.his.dao.domain.MedicalInstit;
import com.his.dao.impl.MedicalInstitDAOImpl;
import com.his.struts.action.common.BaseAction;
import com.his.struts.util.NumberUtils;
import com.his.struts.util.PageInfoBean;
import com.his.struts.util.StringUtils;

public class MedicalInstitAction extends BaseAction {

	MedicalInstitDAO miDao = new MedicalInstitDAOImpl();
	private int pageSize = 20;

	public ActionForward adminAllList(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		if (!super.hasAdminLogin(request)) {
			return mapping.findForward("adminLogin");
		}
		String pageNoStr = request.getParameter("pageNo");
		int pageNo = NumberUtils.toInt(pageNoStr, 1);

		int total = miDao.queryMedicalCount(null);

		PageInfoBean<MedicalInstit> pageInfo = new PageInfoBean<MedicalInstit>(
				pageNo, pageSize, total);

		List<MedicalInstit> list = miDao.queryMedical(null,
				pageInfo.getPageNo(), pageInfo.getPageSize());

		pageInfo.setData(list);

		request.setAttribute("pageInfo", pageInfo);
		return mapping.findForward("adminAllList");
	}

	public ActionForward adminDeleteOne(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		if (!super.hasAdminLogin(request)) {
			return mapping.findForward("adminLogin");
		}
		String idStr = request.getParameter("id");
		if (NumberUtils.isInt(idStr)) {
			miDao.deleteMedical(Integer.parseInt(idStr));
		} else {
			request.setAttribute("message", "id 格式错误");
		}

		return this.adminAllList(mapping, form, request, response);
	}

	public ActionForward queryDetail(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		if (!super.hasAdminLogin(request)) {
			return mapping.findForward("adminLogin");
		}
		String idStr = request.getParameter("id");
		if (NumberUtils.isInt(idStr)) {
			MedicalInstit mi = miDao.queryMedicalInstitById(Integer
					.parseInt(idStr));
			request.setAttribute("mi", mi);
		} else {
			request.setAttribute("message", "id 格式错误");
		}
		return mapping.findForward("toUpdate");
	}

	public ActionForward saveMedical(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		DynaActionForm dyna = (DynaActionForm)form;
		MedicalInstit mi = (MedicalInstit) dyna.get("bean");
		if(!this.validateSave(mi, request)){
			return mapping.findForward("toUpdate");
		}
		miDao.addMedicalInstit(mi);
		request.setAttribute(MESSAGE, "添加成功");
		return this.adminAllList(mapping, form, request, response);
	}
	
	public ActionForward updateMedical(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response){
		DynaActionForm dyna = (DynaActionForm)form;
		MedicalInstit mi = (MedicalInstit) dyna.get("bean");
		if(!this.validateSave(mi, request)){
			return mapping.findForward("toUpdate");
		}
		miDao.updateMedicalInstit(mi);
		request.setAttribute(MESSAGE, "添加成功");
		return this.adminAllList(mapping, form, request, response);
	}
	
	private boolean validateSave(MedicalInstit mi,HttpServletRequest request){
		if(StringUtils.isBlank(mi.getName())){
			request.setAttribute(MESSAGE, "请输入医疗机构名称");
			return false;
		}
		if(StringUtils.isBlank(mi.getServerLimit())){
			request.setAttribute(MESSAGE, "请输入服务范围");
			return false;
		}
		if(StringUtils.isBlank(mi.getProvince())){
			request.setAttribute(MESSAGE, "请选择省");
			return false;
		}
		if(StringUtils.isBlank(mi.getCity())){
			request.setAttribute(MESSAGE, "请选择市");
			return false;
		}
		if(StringUtils.isBlank(mi.getDetailAddr())){
			request.setAttribute(MESSAGE, "请输入详细地址");
			return false;
		}
		if(StringUtils.isBlank(mi.getPhone())){
			request.setAttribute(MESSAGE, "请输入电话号码");
			return false;
		}
		if(StringUtils.isBlank(mi.getDetaildesc())){
			request.setAttribute(MESSAGE, "请输入详细信息");
			return false;
		} 
		return true;
	}
}
